package com.wht.sip.config;

import com.wht.sip.common.R;
import lombok.extern.slf4j.Slf4j;
import org.noear.solon.annotation.Component;
import org.noear.solon.core.exception.StatusException;
import org.noear.solon.core.handle.Context;
import org.noear.solon.core.handle.Handler;
import org.noear.solon.core.route.RouterInterceptor;
import org.noear.solon.core.route.RouterInterceptorChain;

@Slf4j
@Component
public class AppRouterInterceptor implements RouterInterceptor {

    @Override
    public void doIntercept(Context ctx, Handler mainHandler, RouterInterceptorChain chain) throws Throwable {

        try {
            chain.doIntercept(ctx, mainHandler);
        } catch (StatusException e){
            ctx.render(R.error(e.getCode(), e.getMessage()));
//            log.error("", e);
        } catch (Throwable e) {
            ctx.render(R.error(500, e.getMessage()));
            log.error("", e);
        }

    }
}
